<template>
  <div class="child2">
    <h2>我是Child2组件</h2>
    <h3>我有台电脑：{{ computer }}</h3>
    <h3>GrandChild给我的牛奶：{{ niunai }}</h3>
  </div>
</template>

<script>
  export default {
    name:'Child2',
    data() {
      return {
        computer:'联想',
        niunai:''
      }
    },
    methods: {
      getMilk(value){
        console.log('我是Child2，收到了牛奶',value)
        this.niunai = value
      }
    },
    mounted() {
      // 绑定事件并指定回调 —— 接收数据的组件干这事
      this.$bus.$on('send-milk',this.getMilk)
    },
    beforeDestroy() {
      this.$bus.$off('send-milk')
    },
  }
</script>

<style scoped>
  .child2 {
    margin-top: 10px;
    background-color: skyblue;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 0 10px;
  }
</style>